package com.dtwave.utils;

import com.dtwave.constant.FieldConstants;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

public class NumberUtils {

    public static Double add(String v1, String v2) {
        if (StringUtils.isEmpty(v1)) {
            v1 = FieldConstants.STRDOUBLESTRING;
        }
        if (StringUtils.isEmpty(v2)) {
            v2 = FieldConstants.STRDOUBLESTRING;
        }
        return new BigDecimal(v1).add(new BigDecimal(v2)).doubleValue();
    }

    public static String substractToString(String v1, String v2) {
        if (StringUtils.isEmpty(v1)) {
            v1 = FieldConstants.STRDOUBLESTRING;
        }
        if (StringUtils.isEmpty(v2)) {
            v2 = FieldConstants.STRDOUBLESTRING;
        }
        return new BigDecimal(v1).subtract(new BigDecimal(v2)).toPlainString();
    }

    public static String substract(String... nums) {

        String result = FieldConstants.STRDOUBLESTRING;
        if (nums != null) {
            for (int i = 0; i < nums.length; i++) {
                if (i == 0) {
                    result = nums[0];
                    continue;
                } else {
                    result = NumberUtils.substractToString(result, nums[i]);
                }
            }
        }
        return result;
    }

}
